package com.epam.tcs.dao.hibernate.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.epam.tcs.dao.factory.ThemeDAO;
import com.epam.tcs.dao.hibernate.ConnectionFactory;
import com.epam.tcs.dao.hibernate.entities.Theme;

public class ThemeDAOHibernateImpl extends ElementDAOHibernateImpl<Theme>
		implements ThemeDAO {

	public ThemeDAOHibernateImpl(Class objType) {
		this.objType = objType;
	}

	@Override
	public Theme getElement(long id) {
		Session session = ConnectionFactory.getOrInitSession();
		Theme theme = (Theme) session.load(objType, id);

		Hibernate.initialize(theme.getPrograms());
		Hibernate.initialize(theme.getLectures());
		Hibernate.initialize(theme.getPractices());

		session.close();
		return theme;
	}

	@Override
	public List<Theme> getAllElements() {
		Session session = ConnectionFactory.getOrInitSession();
		List<Theme> themes = (List<Theme>) session.createQuery("from " + objType.getName()).list();
		
		for (Theme theme : themes) {
			Hibernate.initialize(theme.getPrograms());			
			Hibernate.initialize(theme.getLectures());
			Hibernate.initialize(theme.getPractices());
		}
		
		session.close();
		return themes;
	}


}
